﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace WindowsFormsApplication2
{
   public class my58Pic : BoundaryPostData
    {
       public my58Pic(CookieCollection ck):base(ck)
       {


       }

       public string username { get; set; }
       public string fileToUpload { get; set; }
       public string DoRequestPost()
       {
           
           String fileFormName = "fileUploadInput";
           String contenttype = "image/pjpeg";

           string boundary = "-----------------------------"+this.TicksStr;

           HttpWebRequest hreq = (HttpWebRequest)HttpWebRequest.Create("http://post.image.58.com/upload");
           hreq.Method = "POST";
           hreq.Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*";

           hreq.ContentType = "multipart/form-data; boundary=" + boundary;
           hreq.Referer = "http://post.58.com/135/12/s5";
           //自己创建的CookieContainer
           hreq.CookieContainer = new CookieContainer();
           hreq.CookieContainer.Add(cklist);

           StringBuilder sb = new StringBuilder();

           sb.Append("--");
           sb.Append(boundary);
           sb.Append("\r\n");
           sb.Append("Content-Disposition: form-data; name=\"");
           sb.Append("name");
           sb.Append("\"; ");
           sb.Append("\r\n");
           sb.Append("\r\n");

           string postHeader = sb.ToString();
           byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
           byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
           //  fileStream = new FileStream(fileToUpload, FileMode.Open, FileAccess.Read);
           byte[] name = Encoding.ASCII.GetBytes(username);
           // length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
           //hreq.ContentLength = length;

           Stream requestStream = hreq.GetRequestStream();
           requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

           requestStream.Write(name, 0, name.Length);
           requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
           sb = new StringBuilder();

           sb.Append("--");
           sb.Append(boundary);
           sb.Append("\r\n");
           sb.Append("Content-Disposition: form-data; name=\"");
           sb.Append("backFunction");
           sb.Append("\"; ");
           sb.Append("\r\n");
           sb.Append("\r\n");

           postHeader = sb.ToString();
           postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
           boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
           //  fileStream = new FileStream(fileToUpload, FileMode.Open, FileAccess.Read);
           name = Encoding.ASCII.GetBytes("SINGLEUP.addImg");
           // length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
           //hreq.ContentLength = length;
           requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

           requestStream.Write(name, 0, name.Length);
           requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);

           sb = new StringBuilder();
           sb.Append("Content-Disposition: form-data; name=\"");
           sb.Append("__pic_dir");
           sb.Append("\"; ");
           sb.Append("\r\n");
           sb.Append("\r\n");

           postHeader = sb.ToString();
           postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
           boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
           //  fileStream = new FileStream(fileToUpload, FileMode.Open, FileAccess.Read);
           name = Encoding.ASCII.GetBytes("p1");
           // length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
           //hreq.ContentLength = length;
           requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

           requestStream.Write(name, 0, name.Length);
           requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);

           sb = new StringBuilder();
           sb.Append("Content-Disposition: form-data; name=\"");
           sb.Append("PicPos");
           sb.Append("\"; ");
           sb.Append("\r\n");
           sb.Append("\r\n");

           postHeader = sb.ToString();
           postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
           boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
           //  fileStream = new FileStream(fileToUpload, FileMode.Open, FileAccess.Read);
           name = Encoding.ASCII.GetBytes("0");
           // length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
           //hreq.ContentLength = length;
           requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

           requestStream.Write(name, 0, name.Length);
           requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);



           sb = new StringBuilder();
           sb.Append("Content-Disposition: form-data; name=\"");
           sb.Append(fileFormName);
           sb.Append("\"; filename=\"");
           sb.Append(Path.GetFileName(fileToUpload));
           sb.Append("\"");
           sb.Append("\r\n");
           sb.Append("Content-Type: ");
           sb.Append(contenttype);
           sb.Append("\r\n");
           sb.Append("\r\n");
           postHeader = sb.ToString();
           postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
           boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "");
           FileStream fileStream = new FileStream(fileToUpload, FileMode.Open, FileAccess.Read);
           // length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
           //hreq.ContentLength = length;
           //Stream requestStream = hreq.GetRequestStream();
           requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

           byte[] buffer = new Byte[(int)fileStream.Length];
           int bytesRead = 0;
           while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
               requestStream.Write(buffer, 0, bytesRead);
           requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
           requestStream.Close();

           HttpWebResponse myResponse = hreq.GetResponse() as HttpWebResponse;

           string str2;
           using (Stream s = myResponse.GetResponseStream())
           {
               StreamReader objReader = new StreamReader(s, Encoding.UTF8);
               str2 = objReader.ReadToEnd();//此处接收到的长度经常为零
           }

           string[] arr = str2.Split('\'');
           if (arr.Length>0)
           {
               this.LastStatus = "发布图片成功";
           }else
           {

               this.LastStatus = "发布图片失败";

           }
           string url = arr[1];

           return url;
       }




   

     

    }
}
